/**
 * 
 */
package com.et114.components.acegi.tag;

import javax.servlet.jsp.JspException;
//import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.acegisecurity.userdetails.UserDetails;

import com.et114.components.acegi.Constants;
import com.et114.components.acegi.cache.AcegiCacheManager;
import com.et114.components.acegi.resourcedetails.ResourceDetails;
import com.et114.core.common.AppContext;
import com.et114.core.utility.StringPool;
import com.et114.modules.org.utility.SessionUserInfo;

import org.springframework.util.Assert;

import org.acegisecurity.GrantedAuthority;

import java.util.*;

/**
 * @author guanhw
 */
public class AuthCompTag extends TagSupport {
	private static final long	serialVersionUID	= - 4882284083730161860L;
	protected Log logger = LogFactory.getLog( this.getClass( ) );
	private AcegiCacheManager cacheManager = null ;
	private String code ; 
	//private String prefix = Constants.RESOURCE_COMPONENT + "_"; 
	
	
	public  AuthCompTag (){
		code = null ;
	}
	
	public void release() {
		code = null ;
	}
	
	public String getCode ( ) {
		return code;
	}

	public void setCode ( String code ) {
		this.code = code;
	}

	public int doStartTag() throws JspException {
		Assert.notNull ( code , "code is null!" ) ; 
		//session 
		javax.servlet.http.HttpSession session = pageContext.getSession ( );
		//check
		
		if ( session.getAttribute ( Constants.SESSION_CURRENT_USER_KEY ) == null )  {
			return SKIP_BODY;
		}
		Object currentName =  ( ( SessionUserInfo )session.getAttribute ( Constants.SESSION_CURRENT_USER_KEY ) ) .getLoginName ( ) ;
		//JspWriter out = null;
		try {
			//out = pageContext.getOut ( ) ;
			//资源
			List list = new ArrayList();
			cacheManager = ( AcegiCacheManager ) AppContext.getBean ( "acegiCacheManager" ) ; 
			ResourceDetails rd = cacheManager.getAuthorityFromCache ( code ) ; 
			
			if ( rd == null ) 
				rd = cacheManager.getAuthorityFromCache ( code + StringPool.COLON ) ;
			
			if ( rd == null ) {
				List comps = cacheManager.getComponents ( ) ; 
				for ( java.util.Iterator iter = comps.iterator ( ) ; iter.hasNext ( ) ; ) {
					String resource = ( String ) iter.next ( ) ;
					if ( resource.startsWith ( code + StringPool.COLON ) ) { 
						rd = cacheManager.getAuthorityFromCache ( resource ) ;
						break ; 
					}
				}
			}
			
			if ( rd != null ) {
				GrantedAuthority[] authorities = rd.getAuthorities ( );
				if ( authorities.length == 0 ) {
					release();
					return EVAL_PAGE;					
				} 
				for  ( int i = 0 ; i < authorities.length ; i ++ ) {
					GrantedAuthority ga = authorities[ i ] ;
					list.add ( ga.toString ( ) ) ;
				}
				 
				// 用户
				UserDetails ud = cacheManager.getUserDetailsByUserId ( currentName.toString ( ) ) ;
				if ( ud != null ) {
					authorities = ud.getAuthorities ( );
					for  ( int i = 0 ; i < authorities.length ; i ++ ) {
						GrantedAuthority ga = authorities[ i ] ;
						String key = ga.getAuthority ( );
						if ( list.contains ( key ) ) {
							release();
							return EVAL_PAGE;
						}
					}
				}
			}
			else {
				release();
				return EVAL_PAGE;
			}
		}
		catch ( Exception e ) {
			logger.error ( e.getMessage ( ) , e  ) ;
			release();
			return SKIP_BODY;			
		}
		release();
		return SKIP_BODY;
	}
	
}
